﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BISPOS
{
    public partial class UpdateStockXtraForm : DevExpress.XtraEditors.XtraForm
    {
        private Product product;
        double diff = 0;
        public UpdateStockXtraForm(Product _product)
        {
            InitializeComponent();
            product = _product;
            
            productTextEdit.Text = product.Name;
            oldQuantityTextEdit.Text = product.StockCount.ToString("N2");

        }

        private void newQuantityTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                diff = Convert.ToDouble(newQuantityTextEdit.Text)
                    - Convert.ToDouble(oldQuantityTextEdit.Text);
                if (diff < 0)
                    diffTextEdit.Text = String.Format("ปรับปรุงออก: {0:N2} {1}", -diff, product.Unit);
                else
                    diffTextEdit.Text = String.Format("ปรับปรุงเข้า: {0:N2} {1}", diff, product.Unit);
            }
            catch { }
        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
            BISPOS.Repository.ProductRepository productRepository = new Repository.ProductRepository();
            
            double newQuantity = product.StockCount + diff;
            if (product.FirstAvailableStock.OriginalQuantity >= newQuantity)
            {
                Stock stock = product.FirstAvailableStock;
                stock.CurrentQuantity += diff;
                db.SubmitChanges();
                MessageBox.Show("ปรับปรุงสำเร็จ", "Success", MessageBoxButtons.OK);
                this.Close();
            }
            else
            {
                MessageBox.Show("ไม่สำเร็จ", "Error", MessageBoxButtons.OK);
            }
        }
    }
}